#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2025/5/28 14:09
# @Author  : Tom_zc
# @FileName: comment.py
# @Software: PyCharm
import sys
import logging
import click
from pathlib import Path

CURR_DIR = Path(__file__).resolve()
ROOT_DIR = CURR_DIR.parent.parent.parent.parent
sys.path.append(ROOT_DIR.as_posix())

from scripts.utils.gitcode_helper import GitCodeHelper
from scripts.utils.parse_params import parse_repo_info_by_webhook_payload
from scripts.utils.common import func_retry

logging.basicConfig(level=logging.INFO,
                    format="%(asctime)s  %(levelname)s:%(message)s")

_COMMENT_TAG_PUBLISH_SUCCESS = "This component has been successfully published."


class PublishTagCommentHelper:

    def __init__(self, owner, repo, token, pr_number):
        self.git_helper = GitCodeHelper(owner, repo, pr_number, token)

    @func_retry()
    def create_comment(self):
        self.git_helper.create_comment({"body": _COMMENT_TAG_PUBLISH_SUCCESS})


@click.command()
@click.option("--gitcode_token", help='tag')
def main(gitcode_token):
    owner, repo, branch, pr_number, _ = parse_repo_info_by_webhook_payload()
    publish_tag_comment_helper = PublishTagCommentHelper(owner, repo, gitcode_token, pr_number)
    logging.info("start to comment pr {}/{}/{}/{}....".format(owner, repo, branch, pr_number))
    publish_tag_comment_helper.create_comment()


if __name__ == '__main__':
    main()
